<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="设备编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devSn">
              <a-input v-model="model.devSn" placeholder="请输入设备编号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devName">
              <a-input v-model="model.devName" placeholder="请输入设备名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devType">
              <j-dict-select-tag type="list" v-model="model.devType" dictCode="dev_type" placeholder="请选择设备类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安装地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devAddress">
              <a-input v-model="model.devAddress" placeholder="请输入安装地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备归属单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devOwner">
              <a-input v-model="model.devOwner" placeholder="请输入设备归属单位"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devAddressDetail">
              <a-input v-model="model.devAddressDetail" placeholder="请输入详细地址"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="方向" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devDirection">
              <a-input v-model="model.devDirection" placeholder="请输入方向"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备坐标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devLocation">
              <a-input v-model="model.devLocation" placeholder="请输入设备坐标"  >
                <a slot="addonAfter" target="_blank" href="https://lbs.amap.com/tools/picker"> 坐标拾取</a>
              </a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devLinkman">
              <a-input v-model="model.devLinkman" placeholder="请输入维护人"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="维护人电话" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devLinkmanTel">
              <a-input v-model="model.devLinkmanTel" placeholder="请输入维护人电话"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备IP" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devIp">
              <a-input v-model="model.devIp" placeholder="请输入设备IP"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备账号密码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devUser">
              <a-input v-model="model.devUser" placeholder="请输入设备账号密码"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-input v-model="model.remarks" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="启用状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devStatus">
              <j-dict-select-tag type="radio" v-model="model.devStatus" dictCode="use_status" placeholder="请选择状态" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'DevicesForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           devSn: [
              { required: true, message: '请输入设备编号!'},
           ],
           devName: [
              { required: true, message: '请输入设备名称!'},
           ],
           devType: [
              { required: true, message: '请输入设备类型!'},
           ],
        },
        url: {
          add: "/iot/devices/add",
          edit: "/iot/devices/edit",
          queryById: "/iot/devices/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>